/**************************************************************************
 *         Copyright(c) 2007 by Sunplus mMedia  Technology Co., Ltd.       *
 *                                                                         *
 *  This software is copyrighted by and is the property of Sunplus mMedia  *
 *  Technology Co., Ltd. All rights are reserved by Sunplus mMedia Techno- *
 *  logy Co., Ltd. This software may only be used in accordance with the   *
 *  corresponding license agreement. Any unauthorized use, duplication,    *
 *  distribution, or disclosure of this software is expressly forbidden.   *
 *                                                                         *
 *  This Copyright notice "M U S T" not be removed or modified without     *
 *  prior written consent of Sunplus mMedia Technology  Co., Ltd.          *
 *                                                                         *
 *  Sunplus mMedia Technology Co., Ltd. reserves the right to modify this  *
 *  software without notice.                                               *
 *                                                                         *
 *  Sunplus mMedia Technology Co., Ltd.                                    *
 *  19-1, Innovation First Road, Science-Based Industrial Park,            *
 *  Hsin-Chu, Taiwan, R.O.C.                                               *
 *                                                                         *
 **************************************************************************/
#include "general.h"

#include "solution.h"
#include "os_msg.h"
#include "initio.h"
#include "key_scan.h"

#include "sp1k_pb_api.h"
#include "sp1k_video_api.h"
#include "sp1k_hal_api.h"
#include "sp1k_util_api.h"
#include "sp1k_aud_api.h"
#include "sp1k_osd_api.h"

#include "app_main.h"
#include "app_video_play.h"
#include "app_still_play.h"
#include "app_audio_play.h"
#include "app_ui_power.h"
#include "app_ui_osd.h"
#include "app_ui_para.h"
#include "app_audio_rec.h"
#include "app_menu_micro.h"

/**************************************************************************
 *                           C O N S T A N T S                            *
 **************************************************************************/

/**************************************************************************
 *                              M A C R O S                               *
 **************************************************************************/

/**************************************************************************
 *                          D A T A    T Y P E S                          *
 **************************************************************************/

/**************************************************************************
 *                         G L O B A L    D A T A                         *
 **************************************************************************/
static UINT8 vidPbSpeed=1;
/**************************************************************************
 *                 E X T E R N A L    R E F E R E N C E S                 *
 **************************************************************************/
extern  SIMAGE xdata pbFile;
//extern xdata UINT8 OsdMaxX;
extern void uiUpdateOSDTime(UINT8 x, UINT8 y, UINT32 time, UINT8 attr);

//-----------------------------------------------------------------------------
//appVideoPlay
//-----------------------------------------------------------------------------
void appVideoPlay (UINT8 msg) USING_0
{
	static UINT8 mode=0;
//	powerBatLv_t batlv;

	switch (msg) {
		case SP1K_MSG_STATE_INIT:
			printf("vid_pb init\n");
			appVideoPlayInit();
			break;
	    	case SP1K_MSG_KEY_PRESS_S1:
			break;
	    	case SP1K_MSG_KEY_PRESS_S2:

			break;
	    	case SP1K_MSG_KEY_PRESS_MENU:
			printf("menu\n");	
			if(sp1kVideoPbStatusGet() == VID_PB_IDLE) {
				osMsgPost(SP1K_MSG_STATE_TO_MENU);
			} else {
				sp1kVideoPbStop();
				//osMsgPost(SP1K_MSG_STATE_TO_STILL_PLAY);
			}
			break;
	    	case SP1K_MSG_KEY_PRESS_UP:
		case SP1K_MSG_MEDIA_PLAY_START:		
			appVideoplayOK();	
			break;
	    	case SP1K_MSG_KEY_PRESS_DOWN:
			break;
	    	case SP1K_MSG_KEY_PRESS_LEFT:
			//printf("left\n");	
			appVideoPlayRWSpeedSet(appVideoPlaySpeedGet());
			//sp1kVideoPbRWSpeedSet(sp1kVideoPbSpeedGet());
			uiUpdateVideoStatus(1, sp1kVideoPbStatusGet());
			break;
	    	case SP1K_MSG_KEY_PRESS_RIGHT:
			//printf("right\n");
			appVideoPlayFWSpeedSet(appVideoPlaySpeedGet());
			//sp1kVideoPbFWSpeedSet(sp1kVideoPbSpeedGet());	
			uiUpdateVideoStatus(1, sp1kVideoPbStatusGet());
			break;
	    	case SP1K_MSG_KEY_PRESS_F1:

			break;
	    	case SP1K_MSG_KEY_PRESS_TELE:
			break;
	    	case SP1K_MSG_KEY_PRESS_WIDE:
			break;
	    	case SP1K_MSG_KEY_PRESS_DISP:
			break;
	    	case SP1K_MSG_KEY_PRESS_PB:
			if(sp1kVideoPbStatusGet() != VID_PB_IDLE) {
				sp1kVideoPbStop();
				appAutoOffEn(1);
				mode = 1;
			}		    	
			//osMsgPost(SP1K_MSG_STATE_TO_STILL_VIEW);	
			break;			
	    	case SP1K_MSG_CARD_PLUG_IN:
			break;
	    	case SP1K_MSG_CARD_PLUG_OUT:
			break;
	    	case SP1K_MSG_USB_PLUG_IN:
			if(sp1kVideoPbStatusGet() != VID_PB_IDLE) {
				sp1kVideoPbStop();
				appAutoOffEn(1);
			if (storageReadyflag==SUCCESS)
				osMsgPost(SP1K_MSG_USB_PLUG_IN);
			}
			else if (storageReadyflag==SUCCESS) {
				if (USB_DBG) {
					sp1kHalUSBSwPlugIn();
				} else {
					osMsgPost(SP1K_MSG_STATE_TO_USB);
				}				
			}
			break;
	    	case SP1K_MSG_USB_PLUG_OUT:
			break;
		
	    	case SP1K_MSG_TV_PLUG_IN:
			if(sp1kVideoPbStatusGet() != VID_PB_IDLE) {
				sp1kVideoPbStop();
				appAutoOffEn(1);
			}
			osMsgPost(SP1K_MSG_TV_PLUG_IN);
			break;
	    	case SP1K_MSG_TV_PLUG_OUT:
			if(sp1kVideoPbStatusGet() != VID_PB_IDLE) {
				sp1kVideoPbStop();
				appAutoOffEn(1);
			}
			osMsgPost(SP1K_MSG_TV_PLUG_OUT);
			break;
		case SP1K_MSG_BAT_LV_UPDATE://jintao.liu 2008-3-13 add for battery action
			uiUpdateBatteryLevel();
			appBattStsHandle();
			break;
		case SP1K_MSG_BAT_EMPTY:
			if(sp1kVideoPbStatusGet() != VID_PB_IDLE) {//2008-7-10 mantis #27681 add here to pre stop pb before power off
				sp1kVideoPbStop();
				appAutoOffEn(1);
				uiUpdateVideoStatus(1, sp1kVideoPbStatusGet());
			}
			uiUpdateOSDBatEmpty();
			osMsgPost(SP1K_MSG_POWER_OFF);
			sp1kBtnDisableAll();//ready to power off, disable all button!
			break;
		case SP1K_MSG_POWER_OFF:
		case SP1K_MSG_KEY_PRESS_POWER:
			if(sp1kVideoPbStatusGet() != VID_PB_IDLE) {
				sp1kVideoPbStop();
				appAutoOffEn(1);
				uiUpdateVideoStatus(1, sp1kVideoPbStatusGet());
			}
			appPowerOffProc();			
			break;
		case SP1K_MSG_MEDIA_PLAY_END:
			appAutoOffEn(1);
			if(mode) {
				switch (appPbPreStateGet())
				{
					case APP_STATE_STILL_VIEW:
						msg = SP1K_MSG_STATE_TO_STILL_VIEW;
						break;	
					case APP_STATE_VIDEO_VIEW:
						msg = SP1K_MSG_STATE_TO_VIDEO_VIEW;
						break;
			
					case APP_STATE_AUDIO_REC:
						sp1kAudioRecTypeSet(AUDIO_TYPE_NORMAL);
						msg = SP1K_MSG_STATE_TO_AUDIO_REC;
						break;
					default:
						msg = SP1K_MSG_STATE_TO_STILL_VIEW;
						break;
				}
				#if(TV_OPTION == TV_PLAY_OPTION) 
				if(appPanelTypeGet() == 0) {
					osMsgPost(SP1K_MSG_STATE_TO_STILL_PLAY);	
				} else {
					osMsgPost(msg);
				}
				#else
				osMsgPost(msg);
				#endif
				mode = 0;
			} else {
				osMsgPost(SP1K_MSG_STATE_TO_STILL_PLAY);	
			}
			uiUpdateVideoStatus(1, sp1kVideoPbStatusGet());
			break;
	    	default:
			//printf("Unknown message:%d\n", (UINT16)msg);
			break;
	}
	return;
}

void appVideoPlayInit(void) USING_0
{
	appMediaPlayDispBufSet(sp1kPbDispBuffGet(1));
	osMsgPost(SP1K_MSG_MEDIA_PLAY_START);		

	return;	
}

void appVideoplayOK(void) USING_0
{
	UINT8 speed;
	UINT8 OsdMaxX, OsdMaxY;

	sp1kOsdLayoutAttrGet(&OsdMaxX, &OsdMaxY);

	speed = sp1kVideoPbSpeedGet();
	if(speed == vid_pb_speed_normal) {
		if(sp1kVideoPbStatusGet() == VID_PB_IDLE) {
			sp1kVideoPbStart(sp1kPbFileIndexByOrderGet(pbFile.dwIndex));
			if(videoPbStatusGet() == VID_PB_RUN) {
				uiUpdateOSDTime(OsdMaxX - 8, 0, 0, 0);
				appAutoOffEn(0);//jintao.liu 2008-4-29 auto power off
				sp1kSpeaker_ENSet(1);
			} else {
				//show file err
				osMsgPost(SP1K_MSG_STATE_TO_STILL_PLAY);	
				return;
			}
		} else {
			appVideoPlayPause();
		}
		
	} else {
		sp1kVideoPbResume();
	}
	uiUpdateVideoStatus(1, sp1kVideoPbStatusGet());
	
	return;
}

void appVideoPlayPause(void)
{
	UINT8 sts ;

	sts = sp1kVideoPbStatusGet();
	if(sts == VID_PB_PAUSE) {
		sp1kVideoPbResume();
	} else if(sts == VID_PB_RUN) {
		sp1kVideoPbPause();
	}
	return;
}

UINT8 appVideoPlaySpeedGet(void)
{
	UINT8 sts;

	sts = sp1kVideoPbStatusGet();
	if(sts == VID_PB_IDLE) {
		return APP_VID_PB_SPD_NORMAL;	
	}else if(sts == VID_PB_RUN){
		vidPbSpeed = APP_VID_PB_SPD2;
		return vidPbSpeed;
	} else {
		switch(vidPbSpeed) {
			case APP_VID_PB_SPD_NORMAL:
				vidPbSpeed = APP_VID_PB_SPD2;
				break;
			case APP_VID_PB_SPD2:
				vidPbSpeed = APP_VID_PB_SPD4;
				break;
			case APP_VID_PB_SPD4:
				vidPbSpeed = APP_VID_PB_SPD8;
				break;
			case APP_VID_PB_SPD8:
				vidPbSpeed = APP_VID_PB_SPD8;
				break;
			default :
				vidPbSpeed = APP_VID_PB_SPD_NORMAL;
				break;
		}
	}
	return vidPbSpeed;
	
}

void appVideoPlayFWSpeedSet(UINT8 spd)
{
	UINT8 speed;
	
	speed = spd;
	if(sp1kVideoPbStatusGet() == VID_PB_RW) {
		speed = APP_VID_PB_SPD2;
	}	
	sp1kVideoPbFWSpeedSet(speed);

	return;
}

void appVideoPlayRWSpeedSet(UINT8 spd)
{
	UINT8 speed;
	
	speed = spd;
	if(sp1kVideoPbStatusGet() == VID_PB_FW) {
		speed = APP_VID_PB_SPD2;
	}	
	sp1kVideoPbRWSpeedSet(speed);

	return;
}
